import { deleteCookie, getCookie, setCookie } from "h3";
import type { EventHandlerRequest, H3Event } from "h3";

/**
 * 清除刷新令牌Cookie
 * @param event H3事件对象
 */
export function clearRefreshTokenCookie(event: H3Event<EventHandlerRequest>) {
  deleteCookie(event, "jwt", {
    httpOnly: true,
    sameSite: "none",
    secure: true,
  });
}

/**
 * 设置刷新令牌Cookie
 * @param event H3事件对象
 * @param refreshToken 刷新令牌字符串
 */
export function setRefreshTokenCookie(
  event: H3Event<EventHandlerRequest>,
  refreshToken: string
) {
  setCookie(event, "jwt", refreshToken, {
    httpOnly: true,
    maxAge: 24 * 60 * 60 * 1000, // 24小时过期时间
    sameSite: "none",
    secure: true,
  });
}

/**
 * 从Cookie中获取刷新令牌
 * @param event H3事件对象
 * @returns 刷新令牌字符串或undefined
 */
export function getRefreshTokenFromCookie(event: H3Event<EventHandlerRequest>) {
  const refreshToken = getCookie(event, "jwt");
  return refreshToken;
}
